package Client;

import Message.Message;
import utils.AccountSendMessageInfoFromOneList;
import utils.AccountSendMessageInfoList;

import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.util.TimerTask;
import java.util.Timer;

public class ChatUI extends JFrame {

	/**创建该聊天窗口的用户*/
	User user;
	/**用于消息的收发*/
	Message message;
	/**创建窗口的用户(From)id*/
	private String chatUICreater;
	/**用户聊天对象的(To)id*/
	private String chatObject;
	/**标识聊天界面的唯一编号*/
	private String chatUIId;
	/**分割聊天信息显示框和消息输入框的对象*/
	private JSplitPane splitPane;
	/**放areaShowChatInfo*/
	private JScrollPane paneShowChatInfo;
	/**放areaInputText*/
	private JScrollPane paneInputText;
	/**显示消息*/
	JTextArea areaShowChatInfo;
	/**输入消息*/
	JTextArea areaInputText;
	/**放按钮的Panel*/
	private JPanel paneButton;
	/**发送消息的按钮*/
	JButton bSend;

	AccountSendMessageInfoFromOneList chatObjectTochatUICreater= new AccountSendMessageInfoFromOneList();
	// 初始化
	public ChatUI(User user,String chatUICreater, String chatObject) {
		this.user=user;
		this.setChatUIId(chatUICreater,chatObject);

		this.setTitle(chatUICreater+"<===>"+chatObject);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(750,600);
		//this.setUndecorated(true);
		this.setMinimumSize(new Dimension(450,450));
		this.setLocationRelativeTo(null);

		ChatUISet();

		this.setChatUIActionListener(new ChatUIActionListener(this));
		this.user.addChatUIList(this);

		this.setVisible(true);

		setAreaShowChatInfo();

	}

	// 调用方法显示窗口
	public void ChatUISet() {

		paneShowChatInfo = new JScrollPane();
		paneShowChatInfo.setMinimumSize(new Dimension(450,200));
		areaShowChatInfo = new JTextArea();
		areaShowChatInfo.setEditable(false);
		DefaultCaret caret = (DefaultCaret)areaShowChatInfo.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		paneShowChatInfo.getViewport().add(areaShowChatInfo);

		paneInputText = new JScrollPane();
		paneInputText.setMinimumSize(new Dimension(450,200));
		areaInputText = new JTextArea();
		paneInputText.getViewport().add(areaInputText);

		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, paneShowChatInfo, paneInputText);
		add(splitPane,BorderLayout.CENTER);

		paneButton = new JPanel();
		bSend = new JButton("Send");

		paneButton.add(bSend);
		add(paneButton,BorderLayout.SOUTH);


	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public void setChatUIActionListener(ChatUIActionListener chatUIActionListener) {
		bSend.addActionListener(chatUIActionListener);
		areaInputText.addKeyListener(chatUIActionListener);
		areaShowChatInfo.addKeyListener(chatUIActionListener);
		this.addWindowListener(chatUIActionListener);
	}

	public String getChatUICreater() {
		return chatUICreater;
	}

	public String getChatObject() {
		return chatObject;
	}

	public void setChatUIId(String chatUICreater, String chatObject){
		this.chatUICreater =chatUICreater;
		this.chatObject =chatObject;
		this.chatUIId=this.chatUICreater +this.chatObject;
	}
	
	public String getChatUIId(){
		return this.chatUIId;
	}

	public void setAreaShowChatInfo(AccountSendMessageInfoFromOneList accountSendMessageInfoFromOneList){
		this.chatObjectTochatUICreater=accountSendMessageInfoFromOneList;
		areaShowChatInfo.setText("");
		areaShowChatInfo.append(this.chatObjectTochatUICreater.toString());
	}

	public boolean getNewMessage(int i){
		return this.user.getMessageListElementLength(i) > this.user.getMessageOneListLength(i);
	}

	public void setAreaShowChatInfo(){

		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				AccountSendMessageInfoList accountSendMessageInfoList=user.getMessageList();
				AccountSendMessageInfoFromOneList accountSendMessageInfoFromOneList;
				int size=accountSendMessageInfoList.size();
				for (int i=0;i<size;i++) {
					accountSendMessageInfoFromOneList=accountSendMessageInfoList.get(i);
					if(accountSendMessageInfoFromOneList.get(0).getUserIdInDatabaseTo().equals(chatUICreater) &&
							accountSendMessageInfoFromOneList.get(0).getUserIdInDatabaseFrom().equals(chatObject) ||
							accountSendMessageInfoFromOneList.get(0).getUserIdInDatabaseTo().equals(chatObject) &&
									accountSendMessageInfoFromOneList.get(0).getUserIdInDatabaseFrom().equals(chatUICreater)){
						AccountSendMessageInfoFromOneList chatObjectTochatUICreater = accountSendMessageInfoList.get(i);
						//if(getNewMessage(i)){
							areaShowChatInfo.setText("");
							areaShowChatInfo.append(chatObjectTochatUICreater.toString());
						//}
						break;
					}
				}
			}
		};
		//schedule(TimerTask task, long delay, long period)
		new Timer().schedule(task,0,1000);//创建 Timer 对象时，也是创建一个线程


	}

}
